<template>
  <div class="login-wrapper">
    <el-row type="flex" class="loginform" justify="center" align="middle">
      <el-col :xs="12" :sm="10" :md="8" :lg="6" :xl="6" class="login-content">
        <el-form label-position="top" :model="loginform" :rules="rules" ref="loginform">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="loginform.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="loginform.password"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm">登陆</el-button>
            <el-button @click="resetForm">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginform: {
        username: "",
        password: ""
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名称", trigger: "blur" },
          { min: 3, max: 6, message: "长度在 3 到 6 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 3, max: 6, message: "长度在 3 到 6 个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    //登陆功能的实现
    login() {
      //使用axios发送请求
      this.$http
        .post("/login", this.loginform)
        .then(res => {
          console.log(res);
          const { data, meta } = res.data;
          console.log(data);
          if (meta.status === 200) {
            console.log("登陆成功");
            localStorage.setItem("token",data.token);
            this.$router.push("/home");
          } else {
            console.log(meta.msg);
            this.$message({
              message: meta.msg,
              type: "error",
              duration: 2000
            });
          }
        });
    },
    submitForm() {
      this.$refs.loginform.validate(valid => {
        if (valid) {
          this.login();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm() {
      this.$refs.loginform.resetFields();
    }
  }
};
</script>

<style>
.login-wrapper,
.loginform {
  height: 100%;
}
.loginform {
  background-color: #2d434c;
}
.login-content {
  min-width: 240px;
  background-color: #fff;
  padding: 20px 35px;
  border-radius: 10px;
}
</style>

